﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CalculatorMVVM.Models;
using System.ComponentModel;
using System.Windows.Input;
using CalculatorMVVM.Command;

namespace CalculatorMVVM.ViewModels
{


    class ViewModelCalculator : INotifyPropertyChanged
    {
        private ICommand additionCommand;
        private ICommand equalsCommand;

        private ModelCalculator calculatorModel;

        public ViewModelCalculator()
        {
            this.calculatorModel = new ModelCalculator();
        }

        public decimal? Result { get; set; }
        public decimal? CurrentValue { get; set; }

        public ICommand EqualsCommand
        {
            get
            {
                if (this.equalsCommand == null)
                {
                    this.equalsCommand = new RelayCommand(() =>
                    {
                        this.SetValue(CurrentValue);
                        this.Calculate();
                        this.CurrentValue = this.Result;
                        this.OnPropertyChanged("CurrentValue");
                    });
                }
                return this.equalsCommand;
            }
            set
            {
                equalsCommand = value;
            }
        }

        public ICommand AdditionCommand
        {
            get
            {
                if (this.additionCommand == null)
                {
                    this.additionCommand = new RelayCommand(() =>
                    {
                        this.SetValue(CurrentValue);
                        this.calculatorModel.Operation = "+";

                    });
                }

                return additionCommand;
            }
            set
            {
                additionCommand = value;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void Calculate()
        {
            decimal? first = this.calculatorModel.First;
            decimal? second = this.calculatorModel.Second;
            string operation = this.calculatorModel.Operation;

            switch (operation)
            {
                case "+":
                    {
                        this.Result = first + second;
                        break;
                    }
                case "-":
                    {
                        this.Result = first - second;
                        break;
                    }
                case "*":
                    {
                        this.Result = first * second;
                        break;
                    }
                case "/":
                    {
                        this.Result = first / second;
                        break;
                    }
                case "log":
                    {
                        this.Result = (decimal)Math.Log((double)first, (double)second);
                        break;
                    }
                case "sqrt":
                    {
                        this.Result = (decimal)Math.Sqrt((double)first);
                        break;
                    }
                case "power":
                    {
                        this.Result = (decimal)Math.Pow((double)first, (double)second);
                        break;
                    }

                default:
                    break;
            }

            OnPropertyChanged("Result");

        }

        public void FillMemory(decimal memory)
        {
            this.calculatorModel.Memory = memory;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void SetValue(decimal? value)
        {
            if (this.calculatorModel.First == null)
            {
                this.calculatorModel.First = value;
            }
            else
            {
                this.calculatorModel.Second = value;
            }
        }
    }
}
